<?php

require dirname(__FILE__).'/../include/common.inc.php';
require WEBFILEPATH.'/include/upload.class.php';
require WEBFILEPATH.'/include/image.class.php';

$tempsavepath = WEBFILEPATH.'/upfiles';
$tempshowpath = '/upfiles';

$action = isset($webrequest['action']) ? trim($webrequest['action']) : '';

if ($action == 'upload') {
	$tempFoldername = '/news/'.global_FormatTime(0, 'Ym');
	$tempsavepath .= $tempFoldername;
	$tempshowpath .= $tempFoldername;

	$tempupconfig = array('type'=>array('gif', 'jpg', 'jpeg', 'png', 'bmp'), 'size'=>2097152);

	$tempupload = new Upload('Filedata', $tempupconfig);
	if (!$tempupload->CheckFile()) {
		echo "ERROR: " . $tempupload->errorMsg;
		exit;
	}
	$tempupload->SaveFile($tempsavepath);
	if (empty($tempupload->fileList)) {
		echo "ERROR: 上传失败";
		exit;
	}

	echo "FILEID:" . $tempshowpath. '/' .$tempupload->fileList[0]['name'];
	exit;
} elseif ($action == 'thumbnail') {
	$objImg = new Image();
	$file = isset($webrequest['file']) ? trim($webrequest['file']) : '';

	$filePath = WEBFILEPATH . $webrequest['file'];

	if ($objImg->SetSrcFile($filePath)) {
		if ($objImg->SetResize(240, 180, 0, 0)) {
			if ($objImg->Resize()) {
				$objImg->ShowFile();
				$objImg->Destroy();
				return TRUE;
			}
		}
	}
	exit;
} elseif ($action == 'delete') {
	$file = isset($webrequest['file']) ? trim($webrequest['file']) : '';
	$filePath = WEBFILEPATH . $webrequest['file'];

	if (@unlink($filePath)) {
		echo "success";
	} else {
		echo "failed";
	}
	exit;
}